﻿using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;


[DisableAutoCreation]
public partial class PlayerInputHendleSystem : SystemBase
{
    protected override void OnCreate()
    {
        RequireForUpdate<InputComp>();
        RequireForUpdate<PlayerTag>();
    }
    
    protected override void OnUpdate()
    {

        var inputEntity = SystemAPI.GetSingleton<InputComp>(); //获取单例
        var PlayerEntity = SystemAPI.GetSingletonEntity<PlayerTag>(); //获取单例


        //foreach (var moveAspect in SystemAPI.Query<MoveAspect>().WithAll<PlayerTag>())
        //{
        //处理速度
        var velComp = SystemAPI.GetComponent<VelComp>(PlayerEntity);
        var transform = SystemAPI.GetComponent<LocalTransform>(PlayerEntity);

        //if(math.abs(Input.GetAxis("Horizontal")) > 0 || math.abs(Input.GetAxis("Vertical")) > 0)
        //{
        //    float2 curInput = new(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
        //    velComp.value.xy = curInput * 10;
        //}
        float2 curInput = inputEntity.Dir;
        velComp.value.xy = curInput * 10;

        //获得朝向鼠标的方向
        //var mousePos = new Vector2(inputEntity.MousePos_x,inputEntity.MousePos_y);
        //var worldPos = _mainCamera.ScreenToWorldPoint(mousePos);
        //var dir = new Vector2(worldPos.x, worldPos.y) - new Vector2(transform.Position.x, transform.Position.y);
        //var angle = dir.Vector2Angle2D();

        SystemAPI.SetComponent(PlayerEntity, velComp);
        //SystemAPI.SetComponent(PlayerEntity, transform);

        // 更新旋转朝向
        //playerEntity.ReplaceRotComp(angle);

        // 处理开火
        //if (inputEntity.inputComp.MainButtonDown)
        //{
        //    playerEntity.AddFireCmdComp(angle, 1);
        //}
        //}

    }
}
